package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ProductBean;
import com.project.bean.SaleCarBean;
import com.project.mapper.IProductMapper;
import com.project.mapper.ISaleCarMapper;
import com.project.service.ISaleCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SaleCarServiceImpl implements ISaleCarService {

    @Autowired
    private ISaleCarMapper saleCarMapper;

    @Override
    public List<SaleCarBean> findById(Integer userId) {
        return saleCarMapper.findById(userId);
    }

    @Override
    public void add(Integer productId,Integer userId) {
        QueryWrapper<SaleCarBean> qw = new QueryWrapper<>();
        qw.eq("fk_productId",productId);
        SaleCarBean saleCarBean = saleCarMapper.selectOne(qw);
        if (saleCarBean == null){
            saleCarMapper.insert(new SaleCarBean(productId,1,userId));
        }else {
            saleCarBean.setNum(saleCarBean.getNum() + 1);
            saleCarMapper.updateById(saleCarBean);
        }
    }

    @Override
    public void updatePriceAndNumById(Integer saleCarId, Integer newNum) {
        SaleCarBean saleCarBean = saleCarMapper.selectById(saleCarId);
        saleCarBean.setNum(newNum);
        saleCarMapper.updateById(saleCarBean);
    }

    @Override
    public void delById(Integer saleCarId) {
        saleCarMapper.deleteById(saleCarId);
    }
}
